<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :modal="true"
      :appendToBody="true"
      width="800px"
      v-dialog-drag
      :close-on-click-modal="false"
      top="5vh"
      :destroy-on-close="true"
      @close="handleClose()"
      @open="handleOpen()"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="ruleForm"
        label-width="120px"
        :disabled="!canEdit"
        v-loading="loading"
      >
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="任务名称" prop="taskName">
              <span>{{ form.taskName }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item :label="isApproveTransfer?'审核人':'执行人'" prop="transferorName">
              <el-input
                v-model="form.transferorName"
                readonly
                :placeholder="isApproveTransfer?'请选择审核人':'请选择执行人'"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="openUserDialog"
                ></el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                :rows="4"
                placeholder="请输入备注"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          v-if="canEdit"
          type="primary"
          :loading="saveloading"
          @click="handleSubmit"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <userList
      :visibles.sync="userDialog.visible"
      :multiple="false"
      :branchinnercode="userDialog.branchinnercode"
      branchinnerKey="code"
      @closeDialog="closeUserDialog"
    ></userList>
  </div>
</template>
<script>
import { checkPermi } from "@/utils/permission";
import { getTaskDetail, saveTask } from "@/api/taskMgmt/center";
export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    row: {
      type: Object,
      default: null,
    },
  },
  data() {
    return {
      loading: false,
      form: {
        taskName: null,
        transferor: null,
        transferorName: null,
        remark: null,
      },
      rules: {
        transferorName: [
          { required: true, message: "请选择执行人", trigger: "change" },
        ],
      },
      userDialog: {
        visible: false,
        branchinnercode: null,
      },
      saveloading: false,
    };
  },
  computed: {
    dialogVisible: {
      set(val) {
        this.$emit("update:visible", val);
      },
      get() {
        return this.visible;
      },
    },
    // 是否审核转办
    isApproveTransfer() {
      return this.row?.hasAuditorPower == "Y";
    },
    title() {
      return this.isApproveTransfer ? "审核转办" : "转办任务";
    },
    canEdit() {
      if (this.row?.id) {
        return checkPermi(["taskMgmt:center:add"]);
      }
      return true;
    },
  },
  methods: {
    handleClose() {
      this.$refs.ruleForm.resetFields();
    },
    handleOpen() {
      this.getDetails();
    },
    getDetails() {
      if (!this.row?.id) {
        return;
      }
      this.loading = true;
      getTaskDetail(this.row.id)
        .then((res) => {
          const { taskName } = res.data;
          this.form.taskName = taskName;
          this.loading = false;
        })
        .catch(() => {
          this.form.taskName = null;
          this.loading = false;
        });
    },
    openUserDialog() {
      // 默认组织筛选条件
      this.userDialog.branchinnercode = this.row.ownedFactory;

      this.userDialog.visible = true;
    },
    closeUserDialog(list) {
      if (!list) {
        return;
      }

      this.form.transferor = list.map((ele) => ele.username).join(",");
      this.form.transferorName = list.map((ele) => ele.realname).join(",");
    },
    handleSubmit() {
      if (!this.row?.id) {
        return;
      }
      this.$refs.ruleForm.validate((flag) => {
        if (flag) {
          this.saveloading = true;
          const { transferor, remark } = this.form;
          const param = {
            id: this.row?.id,
            operateType: "transfer",
            transferor,
            remark,
            classStartTime: this.row.classStartTime,
            classEndTime: this.row.classEndTime,
            className: this.row.className,
            groupName: this.row.groupName,
          };
          saveTask(param)
            .then((res) => {
              this.msgSuccess("保存成功！");
              this.$emit("refresh");
              this.dialogVisible = false;
            })
            .finally(() => {
              this.saveloading = false;
            });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.el-select {
  width: 100%;
}
</style>
